local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseObject());

local  StateClass=require(script.State)
--状态机
----------
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self,id)


    self.StateList={}

	M.super.ctor(self);

end;


function M.dtor(self)
    if  self.UpdateConnect then
        self.UpdateConnect:Disconnect()
        self.UpdateConnect=nil
    end
    for _,v in pairs(self.StateList) do
        v:dtor()
    end
    self.StateList={}

	M.super.dtor(self);
end

function M.SwitchState(self,stateId)
    if self.currentState  then
        if self.currentState:GetId()==stateId then
            return 
        else
            self.currentState:Exit()
            self.currentState=self.StateList[stateId]
            self.currentState:Enter()
        end
    else
        self.currentState=self.StateList[stateId]
        self.currentState:Enter()
    end
end

function M.CreateState(self,stateId)
    local s= StateClass.new(stateId)
    self:AddState(s)
end


function M.AddState(self,state)
    self.StateList[state:GetId()]=state
end

function M.GetCurrentState(self)
    return self.currentState 
end

function M.Update(self)

end


function M.GetState(self,id)
    return self.StateList[id]
end

return M